home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4361 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  82 lines

  1. Path: newsserver.trl.OZ.AU!rhea!aduncan
  2. From: aduncan@rhea.trl.OZ.AU (Allan Duncan)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: 3 bugs in SAS/C v. 6.56
  5. Date: 28 Feb 1996 04:33:57 GMT
  6. Organization: Telecom Research Laboratories, Melbourne, Australia.
  7. Message-ID: <4h0lvl$du2@newsserver.trl.OZ.AU>
  8. References: <4gs3hk$7mp@gjallar.daimi.aau.dk>
  9. NNTP-Posting-Host: rhea.trl.oz.au
  10.  
  11. From article <4gs3hk$7mp@gjallar.daimi.aau.dk>, by liborius@daimi.aau.dk (Per Liboriussen):
  12. > Thus spake Morten B=?iso-8859-2?Q?=F8geskov Jensen <bogeskov@diku.dk>?=:
  13. >>#include <stdio.h>
  14. >>int main(void)
  15. >>{
  16. >>    unsigned short a = 0xffff, b = 0xffff;
  17. >>    if ((a | b) > (unsigned short)0x7fff) {
  18. >>        printf("ok\n");
  19. >>    } else {
  20. >>        printf("bad\n");
  21. >>    }
  22. >>    return 0;
  23. >>}
  24. >>This should work.. But I haven't tested it, since I'm not at home, so I
  25. >>cannot reach out and get SAS/C.
  26. > Neither am I, so the same goes for me. However, a working workaround is
  27. > something like:
  28.  
  29. Well, I got bad for SAS 6.56, and ok for Gcc on the Sun sparc.
  30.  
  31. ...
  32.  
  33. > I don't see what the associativeness of ">" has to do with the above.
  34. > Anyway, when a compiler evaluates a boolean expression such as ">", it
  35. > must perform the usual integral promotions. This means that types narrower
  36. > than int are promoted to int before the comparison. Even in your example
  37. > above the compiler must take the (integer) constant 0x7fff, convert it to
  38.  
  39.                     ^
  40.                 unsigned for ANSI
  41.  
  42. Try this test programme (for a 32 bit int and 2's complement
  43. arithmetic):
  44.  
  45. #include        <stdio.h>
  46.  
  47. #define K 0xFFFFFFFF
  48.  
  49. main()
  50. {
  51. printf( "Int %d, long %d, short %d\n", sizeof( int ), sizeof( long int),
  52.                                         sizeof( short int ) ) ;
  53. if ( K > 0 ) printf( "ANSI C\n" ) ;
  54. else printf( " trad C\n" ) ;
  55. }
  56.  
  57. > unsigned short, and then immediately convert it back to int. Of course,
  58. > the ISO C Standard does give permission for an implementation to carry out
  59. > the comparison by any arcane magic it may wish, as long as a conforming
  60. > program is not able to tell the difference. (The "as if" rule.)
  61. > (Note furthermore that even though a and b are both unsigned shorts, the
  62. > type of the expression (a | b) is int!)
  63.  
  64. a and b are promoted to int under ANSI, and hence a | b is int.  Trad. C
  65. would have promoted a and b to u-int.
  66.  
  67. Adding unsigned short c ;
  68. and setting c = (a | b) before using c in the if changes the SAS
  69. behaviour.  The implication is that SAS are doing a sign extension of a
  70. and b on the int conversion in the if, but not in the assign...
  71. now where is that dissassembler...
  72. Allan Duncan  a.duncan@trl.telstra.com.au  (+613) 9253 6708, Fax 9253 6664
  73.  Photonics & Reference Standards Section
  74.  Telstra Research Labs, Box 249 Rosebank MDC
  75.  Clayton, Victoria, 3169, Australia   (a world renowned Lab in past times)
  76.